﻿
Imports std
Imports centrosphere
Imports command = centrosphere.job.storage.command

Partial Public Class storageJobConsumer
    Private Shared ReadOnly writeunlockrequired_commands() As command = {command.write,
                                                                         command.uniqueWrite,
                                                                         command.delete,
                                                                         command.modify}

    Private Shared Function writeunlockrequired(ByVal i As command) As Boolean
        Return writeunlockrequired_commands.has(i)
    End Function

    Private Shared ReadOnly readunlockrequired_commands() As command = {command.read,
                                                                        command.seek,
                                                                        command.list,
                                                                        command.sizeof,
                                                                        command.keycount}

    Private Shared Function readunlockrequired(ByVal i As command) As Boolean
        Return readunlockrequired_commands.has(i)
    End Function

    Private Shared Function unlock_required(ByVal l As Int32) As Boolean
        Return l = unlocked
    End Function

    Private ReadOnly writeunlock_required As _do(Of storageJob, Boolean) =
        Function(ByRef i As storageJob) As Boolean
            Return unlock_required(writelocked)
        End Function

    Private ReadOnly readunlock_required As _do(Of storageJob, Boolean) =
        Function(ByRef i As storageJob) As Boolean
            Return unlock_required(readlocked)
        End Function

    Private ReadOnly command_required() As vector(Of _do(Of storageJob, Boolean)) = Nothing

    Private Sub initial_command_required()
        For i As command = job.firstOfStorage To command.last - 1
            Dim j As Int64 = 0
            j = i - job.firstOfStorage
            command_required(j) = New vector(Of _do(Of storageJob, Boolean))()
            command_required(j).push_back(storageJob.command_required(j))
            If writeunlockrequired(i) Then
                command_required(j).push_back(writeunlock_required)
            End If
            If readunlockrequired(i) Then
                command_required(j).push_back(readunlock_required)
            End If
        Next
    End Sub

    Private Function required_init(ByVal j As storageJob) As Boolean
        Return storageJob.required_init(command_required, j)
    End Function

    Private Shared Function init_open(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        Return False
    End Function

    Private Shared Function init_close(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        Return False
    End Function

    Private Shared Function init_health(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        Return False
    End Function

    Private Shared Function init_sync(ByVal j As storageJob) As Boolean
        storageJob.result_failed_init(j)
        Return False
    End Function

    Private Shared Function init_version(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        j.key = buildInfo2
        Return False
    End Function

    Private Function init_lockwrite(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        _lockwrite()
        Return False
    End Function

    Private Function init_unlockwrite(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        _unlockwrite()
        Return False
    End Function

    Private Function init_lockread(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        _lockread()
        Return False
    End Function

    Private Function init_unlockread(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        _unlockread()
        Return False
    End Function

    Private Function init_lock(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        _lockread()
        _lockwrite()
        Return False
    End Function

    Private Function init_unlock(ByVal j As storageJob) As Boolean
        storageJob.init(j)
        _unlockread()
        _unlockwrite()
        Return False
    End Function
End Class
